'use strict';
//打开用户信息
YX.fn.personInfo = function() {
	var that = this
	$("body").on("click", "li[data-scene='team'] .panel_avatar", function() {
		var teamID = $(this).parent().data("account")

		that.mysdk.getTeamMembers(teamID, function(err, obj) {
			if(!err) {
				that.cache.setTeamMembers(teamID, obj)
				var members = obj.members
				//群成员不等于2人返回
				if(members.length != 2) {
					return
				}

				var person = members[0].account == userUID ? members[1].account : members[0].account

				if(/^org_.*/.test(person)) {
					//单位账号
					new Promise(function(resolve, reject) {
						var org_account = person
						var org_id = person.split("org_")[1]
						$.ajax({
							type: "get",
							url: API.orgMsg + org_id,
							async: true,
							contentType: "application/json",
							headers: {
								"x-access-token": Cookie.getCookie("token")
							},
							success: function(result) {
								resolve(result.data[0])
							},
							error: function(err) {
								reject(err)
							}
						});
					}).then(function(orgInfo) {
						console.log("获取单位信息成功")
						//console.log(orgInfo)

						$("#infoModal").load("../zdy-view/orgInfo.html", function() {
							$(".name p").html(orgInfo.full_name)
							$(".contact-name").html(orgInfo.contact_name)
							$(".contact-org").html(orgInfo.contact_Node)
							$(".contact-duties").html(orgInfo.contact_title)
							$(".contact-phone").html(orgInfo.contact_phone)

							$(".phone").append(`<li>${ orgInfo.duty_phone_1 }</li>`)
							$(".phone").append(`<li>${ orgInfo.duty_phone_2 }</li>`)
							$(".phone").append(`<li>${ orgInfo.duty_phone_3 }</li>`)
							Modal.show("#infoModal")
							$("#name").dotdotdot({})
							$("#name p").css("position", "absolute")
						})

					}, function(err) {
						console.log(err)
						console.log("获取单位信息失败")
					})
				} else {
					//个人账号
					new Promise(function(resolve, reject) {
						var uid = person
							//测试
						uid = "371"
						$.ajax({
							type: "get",
							url: API.userDetail + uid,
							async: true,
							headers: {
								"x-access-token": Cookie.getCookie("token")
							},
							contentType: "application/json",
							success: function(result) {
								resolve(result.data)
							},
							error: function(err) {
								reject(err)
							}
						});
					}).then(function(userInfo) {
						console.log("获取用户信息成功")
						//console.log(userInfo)
							//调用模态框
						$("#infoModal").load('../zdy-view/personInfo.html', function() {
							$(".name p").html(userInfo.user_name)
							$(".person-org").html(userInfo.full_name)
							$(".person-duties").html(userInfo.title)
							$(".person-phone").html(userInfo.mobile_no_1)
							
							//图像
							let avatar_url = userInfo.ua_avatar_url || userInfo.avatar_url
							let has_passed = userInfo.has_passed
							//审核通过
							if(avatar_url && has_passed == 1){
								$(".photo img").attr("src",API.qiniuDownload + avatar_url)
							}else{
								//默认头像
								$(".photo img").attr("src",API.defaultImage)
							}
							
							Modal.show("#infoModal")
							$("#name").dotdotdot({})
							$("#name p").css("position", "absolute")

						})
					}, function(err) {
						console.log("获取用户信息失败")
						console.log(err)
					})
				}
			} else {
				alert(err.message)
			}
		})

	})

}